<?php
namespace Admin\Controller;
use Admin\AppConst\WebConst;
use Admin\Service\MenuService;
use Admin\Service\RoleService;

class RoleController extends BaseController
{
    private $listUrl;
    public function __construct()
    {
        parent::__construct();
        $this->actionTitle  = '角色';
        $this->assign('actionTitle', $this->actionTitle);

        $this->listUrl  = $this->urlController.'/index';
        $this->assign('listUrl', $this->listUrl);
    }

    /* @function    列表
     * @date        2018-08-01
     * @author      zhangle
     */
    public function index(){
        $list = RoleService::get()->getList();
        $this->assign('list', $list);
        $this->display();
    }

    /* @function    创建
     * @date        2018-08-02
     * @author      zhangle
     */
    public function create(){
        $result = WebConst::DEFAULT_TYPE_EMPTY_ARRAY;
        if(empty($_POST)){
            $list = MenuService::get()->getListZTrees();
            $this->assign('list', json_encode($list));
            $this->display();
            exit;
        }

        //开始创建
        $title      = $this->getParams['title'];                //角色名称
        $content    = $this->getParams['content'];      //说明
        $menuIdList = $this->getParams['menuIdList'];   //权限id 1,2,3,4,5

        if(empty($title)){
            $this->error('请填写角色名称!');
        }
        if(empty($menuIdList)){
            $this->error('请选择权限!');
        }

        $result     =   RoleService::get()->createRole($title, $content, $menuIdList);

        if($result == WebConst::DEFAULT_TYPE_TRUE){
            $this->success('创建成功!', $this->listUrl);
        }else{
            $this->error('创建失败!');
        }

    }

    /* @function    更新
     * @date        2018-08-02
     * @author      zhangle
     */
    public function edit(){

        $id = intval($this->getParams['id']);
        if($id < WebConst::CATEGORY_TYPE_DEFAULT){
            $this->error('id参数错误!');
        }

        if(empty($_POST)){
            //获取详情
            $role   = RoleService::get()->getRoleById($id);
            $list   = MenuService::get()->getListZTrees(explode(',', $role['menu_ids']));
            $this->assign('list', json_encode($list));
            $this->assign('data', $role);
            //获取当前数据

            $this->display();
            exit;
        }

        //开始更新

        $title      = $this->getParams['title'];                //角色名称
        $content    = $this->getParams['content'];      //说明
        $menuIdList = $this->getParams['menuIdList'];   //权限id 1,2,3,4,5

        if(empty($title)){
            $this->error('请填写角色名称!');
        }
        if(empty($menuIdList)){
            $this->error('请选择权限!');
        }
        $result     =   RoleService::get()->editRole($id, $title, $content, $menuIdList);

        if($result == WebConst::DEFAULT_TYPE_TRUE){
            $this->success('更新成功!', $this->listUrl);
        }else{
            $this->error('更新失败!');
        }

    }

    /* @function    删除
     * @date        2018-08-02
     * @author      zhangle
     */
    public function delete(){

        $id = intval($this->getParams['id']);
        if($id < WebConst::CATEGORY_TYPE_DEFAULT){
            $this->error('id参数错误!');
        }


        $result     =   RoleService::get()->deleteRole($id);

        if($result == WebConst::DEFAULT_TYPE_TRUE){
            $this->success('删除成功!', $this->listUrl);
        }else{
            $this->error('删除失败!');
        }

    }
}
